package com.example.zbh.dsliao.ui.activty;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.example.zbh.dsliao.R;

/**
 * Created by zbh on 15/11/23.
 */
public class NewGroupActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
    private EditText edit_group_name, edit_group_introduction;
    private CheckBox cb_public, cb_member_inviter;
    private LinearLayout ll_open_invite;
    private Button save;
    private ImageView back;

    @Override
    protected int getLayOut() {
        return R.layout.activity_new_group;
    }

    @Override
    protected void initView() {
        edit_group_name = findeView(R.id.edit_group_name);
        edit_group_introduction = findeView(R.id.edit_group_introduction);
        cb_public = findeView(R.id.cb_public);
        cb_member_inviter = findeView(R.id.cb_member_inviter);
        ll_open_invite = findeView(R.id.ll_open_invite);
        save = findeView(R.id.save);
        back = findeView(R.id.back);
    }

    @Override
    protected void initData() {
        cb_public.setOnCheckedChangeListener(this);
        save.setOnClickListener(this);
        back.setOnClickListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            ll_open_invite.setVisibility(View.INVISIBLE);
        } else {
            ll_open_invite.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save:
                toGroupPickContactActivity();
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    private void toGroupPickContactActivity() {
        String name = edit_group_name.getText().toString();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "群名不能为空", Toast.LENGTH_SHORT).show();
        } else {
            Intent intent = new Intent();
            intent.putExtra("name", name);
            intent.setClass(this, GroupPickContactActivity.class);
            startActivityForResult(intent, 0);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("yyyyyyyy", "ssssssssss");
        if (resultCode == Activity.RESULT_OK) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String groupName = edit_group_name.getText().toString();
                    String desc = edit_group_introduction.getText().toString();
                    String[] members = data.getStringArrayExtra("list");

                    try {
                        if (cb_public.isChecked()) {
                            EMGroupManager.getInstance().createPublicGroup(groupName, desc, members, true, 200);
                        } else {
                            EMGroupManager.getInstance().createPrivateGroup(groupName, desc, members, cb_member_inviter.isChecked(), 200);
                        }

                        finish();
                    } catch (EaseMobException e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }
    }
}
